package com.qiankan.qmsj.utils;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Jie Zhang on 2016/9/9.
 */
public class StringUtils {

    /**
     * 检查字符中是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
//        Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean isPhone(String phoneNumber) {
        boolean isValid = false;
        CharSequence inputStr = phoneNumber;
        //正则表达式
        String phone = "^1[34578]\\d{9}$";
        Pattern pattern = Pattern.compile(phone);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 判断密码格式
     *
     * @param //email
     */
    public static boolean isPassword(String password) {
        // Pattern p = Pattern.compile("(?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[_#@]+$).{6,20}");
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$");
        Matcher pm = p.matcher(password);
        if (pm.matches()) {
            return true;
        }
        return false;
    }

    public static boolean isIdentity(String idNumber) {
        boolean isValid = false;
        Pattern pattern = Pattern.compile("^\\d{15}$|^\\d{17}[0-9Xx]$");
        Matcher matcher = pattern.matcher(idNumber);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 设置字符串某些字体的颜色
     *
     * @param str
     * @param ch1
     * @param ch2
     * @param color
     * @param tv
     */
    public static void setTVColor(String str, char ch1, char ch2, int color, TextView tv) {
        int a = str.indexOf(ch1) + 1; //从字符ch1的下标开始
        int b = str.indexOf(ch2); //到字符ch2的下标+1结束,因为SpannableStringBuilder的setSpan方法中区间为[ a,b )左闭右开
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        builder.setSpan(new ForegroundColorSpan(color), a, b, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(builder);
    }

    /**
     * 将大于10000的数字字符串转换为万未单位
     *
     * @param value
     * @return
     */
    public static String convertNum(String value) {
        try {
            double v = Double.parseDouble(value);
            if (v > 10000) {
//                String str = String.valueOf(v / 10000);
                String str = String.format("%.2f", v / 10000);
                return removeLast0(str) + "万";
            } else {
                return value;
            }
        } catch (Exception e) {
            return value;
        }
    }

    private static String removeLast0(String string) {
        if (TextUtils.isEmpty(string)) {
            return "0";
        }
        try {
            while (string.charAt(string.length() - 1) == '0') {
                string = string.substring(0, string.length() - 1);
            }
            if (string.charAt(string.length() - 1) == '.') {
                string = string.substring(0, string.length() - 1);
            }
        } catch (Exception e) {
            return "";
        }
        return string;
    }
}
